package org.celery.modules.other.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.celery.modules.user.entity.UserDomain;
import org.celery.modules.user.service.IUserDomainService;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.Tenant;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.celery.modules.other.entity.Pixel;
import org.celery.modules.other.vo.PixelVO;
import org.celery.modules.other.wrapper.PixelWrapper;
import org.celery.modules.other.service.IPixelService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;

/**
 * 像素表 控制器
 *
 * @author Celery
 * @since 2022-04-15
 */
@RestController
@AllArgsConstructor
@RequestMapping("celery-other/pixel")
@Api(value = "像素表", tags = "像素表接口")
public class PixelController extends BladeController {

	private final IPixelService pixelService;
	private final IUserDomainService userDomainService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入pixel")
	public R<PixelVO> detail(Pixel pixel) {
		Pixel detail = pixelService.getOne(Condition.getQueryWrapper(pixel));
		return R.data(PixelWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 像素表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入pixel")
	public R<IPage<PixelVO>> list(Pixel pixel, Query query, BladeUser bladeUser) {
		IPage<Pixel> pages = pixelService.page(
				Condition.getPage(query),
				Condition.getQueryWrapper(pixel).lambda()
						.eq(Pixel::getCreateUser, bladeUser.getUserId())
		);
		return R.data(PixelWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 像素表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入pixel")
	public R<IPage<PixelVO>> page(PixelVO pixel, Query query) {
		IPage<PixelVO> pages = pixelService.selectPixelPage(Condition.getPage(query), pixel);
		return R.data(pages);
	}

	/**
	 * 新增 像素表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入pixel")
	public R save(@Valid @RequestBody Pixel pixel) {
		return R.status(pixelService.save(pixel));
	}

	/**
	 * 修改 像素表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入pixel")
	public R update(@Valid @RequestBody Pixel pixel) {
		return R.status(pixelService.updateById(pixel));
	}

	/**
	 * 新增或修改 像素表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入pixel")
	public R submit(@Valid @RequestBody Pixel pixel) {
		return R.status(pixelService.saveOrUpdate(pixel));
	}

	
	/**
	 * 删除 像素表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(pixelService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 可选域名列表
	 */
	@GetMapping("/domain-select")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "分页", notes = "传入pixel")
	public R<List<UserDomain>> domainSelect(BladeUser bladeUser) {
		List<UserDomain> domainList = userDomainService.list(Wrappers.<UserDomain>lambdaQuery()
				.eq(UserDomain::getUserId, bladeUser.getUserId())
		);
		return R.data(domainList);
	}
}
